package young.zhang.api;

import young.zhang.domain.AssHole;
import young.zhang.domain.GameInfo;
import young.zhang.domain.Shit;
import young.zhang.domain.enums.AssHoleMovementEnum;
import young.zhang.service.GameRuler;
import young.zhang.util.IEatableComparator;

import java.awt.*;
import java.util.*;
import java.util.List;

/**
 * Created by Administrator on 2018/12/13.
 */
public class AssHoleBeingImpl implements IAssHoleBeing {
    //计数器
    private static int setCount = 0;
    //权重值
    private static int weight= 0;
    IEatableComparator iEatableComparator = new GameRuler();

    public AssHoleMovementEnum onNextMove(AssHole myAssHole, List<Shit> inVisionShitList, List<AssHole> otherAssHoleList, GameInfo gameInfo, IEatableComparator iEatableComparator) {
        try{
            /**
             *
             * 1.寻找距离最短得球，如果距离相同则寻找level高的 ()。
             * 2.判断其他assHole 距离是否比我接近，如果到达步数与我一致则去吃掉，如果比我大致，则回到第1步()
             * 2.判断其他球
             */

            //寻找距离最短得球
            Map map = calculateStep(myAssHole, inVisionShitList, otherAssHoleList);
            for (Shit targetShit: (List<Shit>) map.get("shitDiscount")) {
                if(targetShit.isEatable()){
                    AssHoleMovementEnum res = moveStep(myAssHole,targetShit);
                    if(AssHoleMovementEnum.STAY==moveStep(myAssHole,targetShit)){
                        continue;
                    }else{
                        return res;
                    }
                }
            }
            Random random = new Random(4);
            return  AssHoleMovementEnum.values()[random.nextInt()];
            //判断周围是否有危险 ？  如果有则排除当前路径
        }catch (Exception e){
            e.printStackTrace();
            Random random = new Random(4);
            return  AssHoleMovementEnum.values()[random.nextInt()];
        }
    }

    Map calculateStep(AssHole myAssHole, List<Shit> inVisionShitList, List<AssHole> otherAssHoleList){
        Double x = myAssHole.getAssHoleCoordinate().getX();
        Double y = myAssHole.getAssHoleCoordinate().getY();

        Map discountMap = new HashMap<>();

        List<Shit> shitDiscount = new ArrayList<>();
        for (Shit shit : inVisionShitList) {
            Double discount = calculateDiscount(myAssHole.getAssHoleCoordinate(),shit.getshitCoordinate());
            shit.setEatable(            iEatableComparator.canIHaveShit(myAssHole,shit));
            shit.setDiscount(discount);
            shitDiscount.add(shit);
        }
        List<AssHole> assHoleDiscount = new ArrayList<>();
        for (AssHole assHole : otherAssHoleList) {
            Double discount = calculateDiscount(myAssHole.getAssHoleCoordinate(),assHole.getAssHoleCoordinate());
            assHole.setEatable(iEatableComparator.canIHaveShit(myAssHole,assHole));
            assHole.setDiscount(discount);
            assHoleDiscount.add(assHole);
        }


        Collections.sort(assHoleDiscount, (o1,o2)->{return o1.getDiscount().compareTo(o2.getDiscount());});

        Collections.sort(shitDiscount,(o1,o2)->{
            return  o1.getDiscount().compareTo(o2.getDiscount());
        });

        discountMap.put("assHoleDiscount",assHoleDiscount);
        discountMap.put("shitDiscount",shitDiscount);

    return discountMap;
    }

    Double calculateDiscount(Point point1,Point point2){
        if(point1!=null  && point2!=null ){
            Double b =  Math.abs(point1.getY()-point2.getY());
            Double a = Math.abs(point1.getX()-point2.getX());
            return  a+b;
        }
        else{
            throw new RuntimeException("this point is null");
        }
    }

    AssHoleMovementEnum moveStep(AssHole myAssHole,Shit targetShit){


        Point point1 = myAssHole.getAssHoleCoordinate();
        Point point2 = targetShit.getShitCoordinate();

        if(point2.getX()<point1.getX()){
            return AssHoleMovementEnum.LEFT;
        }else if(point2.getX()>point1.getX()) {
            return AssHoleMovementEnum.RIGHT;
        }
        else{
            if(point2.getY()<point1.getY()){
                return AssHoleMovementEnum.DOWN;
            }else if(point2.getY()>point1.getY()) {
                return AssHoleMovementEnum.UP;
            }else{
                return AssHoleMovementEnum.STAY;
            }
        }
    }





}


class testA{
    public static void main(String[] args) {
        IAssHoleBeing assHoleBeing = new AssHoleBeingImpl();

        for (int i = 0; i < 10; i++) {
            assHoleBeing.onNextMove(null,null,null,null,null);
        }
    }
}
